<?php
	include("mysql_connect.php");
	$xml = simplexml_load_file('php://input');
	
	foreach( $xml->attributes( ) as $attr)
	{
		$AttrArray[] = $attr;
	}
	
	$UserName = $AttrArray[0];
	$UserChannel = $AttrArray[1];
	$UserWeddingDate = $AttrArray[2];
	
	//$TwitterAcc = $xml->children();
	foreach( $xml->children() as $child)
	{
		switch ($child->getName())
		{
			case "Facebook":
				foreach( $child->attributes( ) as $attr)
					$Facebook[] = (string)$attr;
				break;
			case "Twitter":
				foreach( $child->attributes( ) as $attr)
					$Twitter[] = (string)$attr;
				break;
			case "Tumblr":
				foreach( $child->attributes( ) as $attr)
					$Tumblr[] = (string)$attr;
				break;
			case "Instagram":
				foreach( $child->attributes( ) as $attr)
					$Instagram[] = (string)$attr;
				break;
			case "GoogleFeed":
				$attributes = $child->attributes();
				$GoogleFeedTitle[] = (string)$attributes[0];
				$GoogleFeedContent[] = (string)$attributes[1];
				$GoogleFeedLink[] = (string)$attributes[2];
				$GoogleFeedSwitch[] = (int)$attributes[3];
			
				//foreach( $child->attributes['Title'] as $attr)
				break;
		}
	}
	
	$strSqlCommand = "SELECT idCHANNELS, Name
								FROM channels
								WHERE Name = '".$UserChannel."'";
								
	$result = mysql_query($strSqlCommand);
	$rowUserChannel = @mysql_fetch_array($result);
	
	
	$strSqlCommand = "UPDATE user SET";
	
	$strSqlCommand.= " Enable=1,";
	
	if(isset($Facebook) && strlen($Facebook[0])>0)
		$strSqlCommand.= " FaceBookID='".$Facebook[0]."', FaceBookToken='".$Facebook[1]."', FaceBookSwitch='".$Facebook[2]."',";
	if(isset($Twitter) && strlen($Twitter[0])>0)
		$strSqlCommand.= " TwitterKey='".$Twitter[0]."', TwitterSecret='".$Twitter[1]."', TwitterSwitch='".$Twitter[2]."',";
	if(isset($Tumblr) && strlen($Tumblr[0])>0)
		$strSqlCommand.= " TumblrName='".$Tumblr[0]."', TumblrSwitch='".$Tumblr[1]."',";
	if(isset($Instagram) && strlen($Instagram[0])>0)
		$strSqlCommand.= " InstagramToken='".$Instagram[0]."', InstagramSwitch='".$Instagram[1]."',";
	if(isset($UserWeddingDate) && strlen($UserWeddingDate)>0)
		$strSqlCommand.= " WeddingDate='".$UserWeddingDate."',";
	if(isset($UserChannel) && strlen($UserChannel)>0)
		$strSqlCommand.= " Default_Channel='".$rowUserChannel['idCHANNELS']."',";
	
	//Check last character.
	if ($strSqlCommand[strlen($strSqlCommand)-1]==",")
		$strSqlCommand = substr_replace($strSqlCommand,"",-1);
		
	$strSqlCommand.= " WHERE Email='".$UserName."'";
	
	ConnectDB($strSqlCommand);
	
	/*********Test*********/
	//echo $strSqlCommand;
	
	$strSqlCommand = "SELECT idUSER, Name, Level, Organization, Default_Channel
							FROM user
							WHERE Email = '".$UserName."'";
								
	$result = mysql_query($strSqlCommand);
	$rowUserInfo = @mysql_fetch_array($result);
		
	$strSqlCommand = "DELETE FROM spot_user_googlefeed WHERE USER_idUSER = '".$rowUserInfo['idUSER']."'";
	ConnectDB($strSqlCommand);
	
	if (isset($GoogleFeedTitle[0])) {	
		for ($i=0; $i<count($GoogleFeedTitle); $i++)
		{
			$strSqlCommand = "INSERT INTO spot_user_googlefeed (USER_idUSER, Title, Description, URL, Switch) VALUES ('".$rowUserInfo['idUSER']."',
								'".$GoogleFeedTitle[$i]."','".$GoogleFeedContent[$i]."','".$GoogleFeedLink[$i]."','".$GoogleFeedSwitch[$i]."')";
			ConnectDB($strSqlCommand);
		}
	}
	
	echo "<Success Message=\"The requested Information was accepted\" Domain=\"UploadAccountInfo\" />";
	
	
	function ConnectDB($strSqlCommand) {
		
		if (!$result = mysql_query($strSqlCommand))
 		{
	 		echo mysql_error();
			return;
 		}
	}
?>